#pragma once

#ifndef __HWINAPPLICATION_H__
#define __HWINAPPLICATION_H__

#include "hwindef.h"
#include "hwinmessage.h"
#include "hwinform.h"
#include "hwinclass.h"


namespace harlinn
{
    namespace windows
    {

        class MessageLoop : public Object
        {
        public:
            typedef Object Base;
            HWIN_EXPORT MessageLoop();
            HWIN_EXPORT virtual ~MessageLoop();

            HWIN_EXPORT virtual int Run();
        protected:
            HWIN_EXPORT virtual int GetMessage(Message& message);
            HWIN_EXPORT virtual int TranslateMessage(Message& message);
            HWIN_EXPORT virtual Message::Result DispatchMessage(const Message& message);
        };



        class Application : public Component
        {
            friend class Control;

            static Application *currentApplication;

            std::shared_ptr<WindowClasses> windowClasses;

            std::map<HWND, Control*> controls;

            HWIN_EXPORT void AddControl(HWND hWnd, Control* control);
            HWIN_EXPORT void RemoveControl(HWND hWnd);
            HWIN_EXPORT Control* GetControl(HWND hWnd);

        public:
            typedef Component Base;
            HWIN_EXPORT Application( );
            HWIN_EXPORT virtual ~Application();

            HWIN_EXPORT static std::shared_ptr<Application> Current();

            HWIN_EXPORT virtual int Run(std::shared_ptr<Form> mainform );

            HWIN_EXPORT virtual int Run(std::shared_ptr<Form> mainform, std::shared_ptr<MessageLoop> messageLoop );

            HWIN_EXPORT std::shared_ptr<const WindowClasses> GetWindowClasses() const;
            HWIN_EXPORT std::shared_ptr<WindowClasses> GetWindowClasses();
            HWIN_EXPORT static std::shared_ptr<WindowClass> GetWindowClass(const String& theClassName );
            HWIN_EXPORT static std::shared_ptr<WindowClass> GetWindowClass(const wchar_t* theClassName );
        };
    };
};




#endif //__HWINAPPLICATION_H__
